import 'package:equatable/equatable.dart';

part 'errors.dart';

const kMinPasswordLength = 8;
const kMaxPasswordLength = 64;

abstract class ValidatePassword {
  PasswordValidationError? call({
    required String password,
  });
}

class ValidatePasswordImpl implements ValidatePassword {
  const ValidatePasswordImpl();

  @override
  PasswordValidationError? call({
    required String password,
  }) {
    if (password.length < kMinPasswordLength || password.length > kMaxPasswordLength) {
      return const WrongLengthPasswordValidationError();
    }

    final allowedCharactersRegExp = RegExp(r'^[!"#$%&()*+,\-./:;<=>?@\[\]\\^_{}|~`a-zA-Z0-9]+$');

    if (!allowedCharactersRegExp.hasMatch(password)) {
      return const WrongCharacterPasswordValidationError();
    }

    if (!RegExp('[a-zA-Z]+').hasMatch(password)) {
      return const MissingRequiredCharacterPasswordValidationError();
    }

    return null;
  }
}
